Element.Events.extend({
    'wheelup': {
        base: Element.Events.mousewheel.base,
        condition: function(event){
            event = new Event(event);
            if (event.wheel >= 0) 
                this.fireEvent('wheelup', event)
        }
    },
    
    'wheeldown': {
        base: Element.Events.mousewheel.base,
        condition: function(event){
            event = new Event(event);
            if (event.wheel <= 0) 
                this.fireEvent('wheeldown', event)
        }
    }
});
Element.implement({
    getBorderSizes: function(){
        return {
            top: this.getStyle('border-top-width').toInt(),
            right: this.getStyle('border-right-width').toInt(),
            bottom: this.getStyle('border-bottom-width').toInt(),
            left: this.getStyle('border-left-width').toInt()
        }
    },
    getMarginSizes: function(){
        return {
            top: this.getStyle('margin-top').toInt(),
            right: this.getStyle('margin-right').toInt(),
            bottom: this.getStyle('margin-bottom').toInt(),
            left: this.getStyle('margin-left').toInt()
        }
    },
    getPaddingSizes: function(){
        return {
            top: this.getStyle('padding-top').toInt(),
            right: this.getStyle('padding-right').toInt(),
            bottom: this.getStyle('padding-bottom').toInt(),
            left: this.getStyle('padding-left').toInt()
        }
    },
    getClientSize: function(){
        var getWindowSize = function(){
            var w = 0, h = 0;
            if (!window.innerWidth) {
                if (!(document.documentElement.clientWidth == 0)) {
                    w = document.documentElement.clientWidth;
                    h = document.documentElement.clientHeight;
                }
                else {
                    w = document.body.clientWidth;
                    h = document.body.clientHeight;
                }
            }
            else {
                w = window.innerWidth;
                h = window.innerHeight;
            }
            return {
                'width': w,
                'height': h
            };
        }
        if (this == document.body) 
            return getWindowSize();
        return {
            'width': this.getSize().x,
            'height': this.getSize().y
        };
    },
    getBoundWidth: function(){
        var cs = this.getClientSize();
        var ms = this.getMarginSizes();
        var bs = this.getBorderSizes();
        var ps = this.getPaddingSizes();
        return cs.width + ms.left + ms.right + bs.left + bs.right + ps.left + ps.right;
    },    
    getBoundHeight: function(){
        var cs = this.getClientSize();
        var ms = this.getMarginSizes();
        var bs = this.getBorderSizes();
        var ps = this.getPaddingSizes();
        return cs.height + ms.top + ms.bottom + bs.top + bs.bottom + ps.top + ps.bottom;
    },
    getBoundSize: function(){
        var cs = this.getClientSize();
        var ms = this.getMarginSizes();
        var bs = this.getBorderSizes();
        var ps = this.getPaddingSizes();
        return {
            'width': cs.width + ms.left + ms.right + bs.left + bs.right + ps.left + ps.right,
            'height': cs.height + ms.top + ms.bottom + bs.top + bs.bottom + ps.top + ps.bottom
        };
    },
    setBoundWidth: function(boundWidth){
        var ms = this.getMarginSizes();
        var bs = this.getBorderSizes();
        var ps = this.getPaddingSizes();
        var w = Math.max(boundWidth - bs.left - bs.right - ms.left - ms.right - ps.left - ps.right, 0);
        this.setStyle('width', w);
        
    },
    setBoundHeight: function(boundHeight){
        var ms = this.getMarginSizes();
        var bs = this.getBorderSizes();
        var ps = this.getPaddingSizes();
        var h = Math.max(boundHeight - bs.top - bs.bottom - ms.top - ms.bottom - ps.top - ps.bottom, 0);
        this.setStyle('height', h);
        
    },
    setRect: function(rect){
        var ms = this.getMarginSizes();
        var bs = this.getBorderSizes();
        var ps = this.getPaddingSizes();
        var w = Math.max(rect.width - bs.left - bs.right - ms.left - ms.right - ps.left - ps.right, 0);
        var h = Math.max(rect.height - bs.top - bs.bottom - ms.top - ms.bottom - ps.top - ps.bottom, 0);
        this.setStyles({
            'left': rect.left,
            'top': rect.top,
            'width': w,
            'height': h
        });
    },
    destroy: Browser.Engine.trident ? function(){
        var d;
        return function(){
            if (this.tagName != 'BODY') {
                var elements = $A(this.getElementsByTagName('*'));
                elements.each(function(element){
                    $try(Element.prototype.destroy, element);
                });
                Garbage.kill(this);
                d = d || document.createElement('div');
                d.appendChild(this);
                d.innerHTML = '';
            }
        }
    }
() : function(){
        if (this.parentNode && this.tagName != 'BODY') {
            Garbage.kill(this.empty().dispose());
            return null;
        }
    }
});

Function.implement({ 
    times: function(n, bind){
        var count = 0;
        var fn = function(){
            if (count++ < n) 
                return this.run(arguments, bind);
        }
.bind(this);
        return fn;
    },
    once: function(bind){
        return this.times(1, bind);
    },
    after: function(delay, bind, args){
        return this.create({
            'delay': delay,
            'bind': bind,
            'arguments': args
        });
    }
    
});


window['opal'] = $pick(window['opal'], {});
opal.namespace = function(namespace){
    var names = namespace.split('.');
    var currentNode = window;
    names.each(function(item, index){
        currentNode[item] = $pick(currentNode[item], {});
        currentNode = currentNode[item];
    });
};

opal.namespace("opal.widget");

opal.widget.Initialize = function(options){
    var opt = new Options();
    opt.setOptions({
            renderOnInit: true,
			disabled: false,
			hide: false,
			hideMode: 'display',
			onFocus: $empty,
			onBlur: $empty,
			onSelect: $empty,
			onUnselect: $empty,
			onBeforeShow: $empty,
			onBeforeHide: $empty,
			onDestroy: $empty
		});
	opt.setOptions(options);
	$extend(this, opt);	
	
	for (optItem in this.options) {
		var prefix = optItem.substring(0, 2);
		if (prefix === 'on') {
		    this.addEvent(optItem, this.options[optItem]);	
		}
	};
				
	this.options.id = $pick(options.id, opal.generateId());
	this.id = this.options.id;
	
	this.isFocused = function(){
		return this.hasClass('focused');
	};
	
	$wset(this);
			
};

opal.widget.Render = function(obj){
    var Oldel = $(this.id);
    if (Oldel) {
        obj.replaces(Oldel);
        Oldel.destroy();
    }
    else {
        var parent = $(this.options.parent);
        if (parent) {
            parent.adopt(obj);
        }
        else {
             console.log('no parent defined for ' + obj.id);
        }
    }
	
//	$wset(this);
};


(function(){
    var focusManager = {
        currentFocusedId: null,
        manage: function(widget){
            $try(function(){
                if (widget.hasClass('focusManaged')) {
                    widget.addEvents({
                        'focus': function(w){
                            if ($defined(this.currentFocusedId) && $defined($(this.currentFocusedId))) {
                                $try($(this.currentFocusedId).removeClass, $(this.currentFocusedId), 'focused');
                            }
                            $try(w.addClass, w, 'focused');
                            this.currentFocusedId = w.id;
							$log(w);
                        }
.bind(this)                        ,
                        'blur': function(w){
                            this.currentFocusedId = null;
                        }
.bind(this)
                    });
                }
            }
.bind(this));
        }
    };
	
	    $wset = function(widget){
        if ($defined(widget.id)) {
            focusManager.manage(widget);
        }
        return widget;
    };
})()


//include css file
IncludeJCSSFile = function (cssFile){
  document.write('<link rel="stylesheet" type="text/css" href="' + cssFile + '"/>');
};

//includ js file
IncludeJavaScript = function (jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></script>'); 
};


//idGenerator
opal.generateId = function(prefix){
    prefix = $pick(prefix, 'opal-gen');
    
    var id = prefix + opal.generateId.id_counter++;
    
    return id;
};
opal.generateId.id_counter = 0;

//baseClass
opal.widget.Initialize = function(options){
    var opt = new Options();
    opt.setOptions({
            renderOnInit: true,
			disabled: false,
			hide: false,
			hideMode: 'display',
			onFocus: $empty,
			onBlur: $empty,
			onSelect: $empty,
			onUnselect: $empty,
			onBeforeShow: $empty,
			onBeforeHide: $empty,
			onDestroy: $empty
		});
	opt.setOptions(options);
	$extend(this, opt);	
	
	for (optItem in this.options) {
		var prefix = optItem.substring(0, 2);
		if (prefix === 'on') {
		    this.addEvent(optItem, this.options[optItem]);	
		}
	};
				
	this.options.id = $pick(options.id, opal.generateId());
	this.id = this.options.id;
	
	this.isFocused = function(){
		return this.hasClass('focused');
	};
	
	$wset(this);
			
};

opal.widget.Render = function(obj){
    var Oldel = $(this.id);
    if (Oldel) {
        obj.replaces(Oldel);
        Oldel.destroy();
    }
    else {
        var parent = $(this.options.parent);
        if (parent) {
            parent.adopt(obj);
        }
        else {
             console.log('no parent defined for ' + obj.id);
        }
    }
	
//	$wset(this);
};

opal.widget.path = new Object();
opal.widget.path.image = 'img/';


(function(){
    var focusManager = {
        currentFocusedId: null,
        manage: function(widget){
            $try(function(){
                if (widget.hasClass('focusManaged')) {
                    widget.addEvents({
                        'focus': function(w){
                            if ($defined(this.currentFocusedId) && $defined($(this.currentFocusedId))) {
                                $try($(this.currentFocusedId).removeClass, $(this.currentFocusedId), 'focused');
                            }
                            $try(w.addClass, w, 'focused');
                            this.currentFocusedId = w.id;
							$log(w);
                        }
.bind(this)                        ,
                        'blur': function(w){
                            this.currentFocusedId = null;
                        }
.bind(this)
                    });
                }
            }
.bind(this));
        }
    };
	
	    $wset = function(widget){
        if ($defined(widget.id)) {
            focusManager.manage(widget);
        }
        return widget;
    };
})()

function destroyLoading(ctrl){
	if (typeof(ctrl) === 'object') {
		ctrl.remove();
	}	
}
function onIEWinResize(event) {
	function parseWidth(val) {return (isNaN(parseInt(val,10))?0:parseInt(val,10));}
	if(!event) {event=window.event;} var i,cs,parent=this, div=parent.getElementsByTagName("div");
	if(div.length>0) {
		if(parent.currentStyle){cs=parent.currentStyle;}else if(document.defaultView&&document.defaultView.getComputedStyle){cs=document.defaultView.getComputedStyle(parent,"");}else{cs=parent.style;}
		for(i=0; i<div.length; i++) {
			if(div[i].className=='buzy_ele') {
				div[i].style.height=(parent.offsetHeight-parseWidth(cs.borderBottomWidth)-parseWidth(cs.borderTopWidth));
				div[i].style.width=(parent.offsetWidth-parseWidth(cs.borderLeftWidth)-parseWidth(cs.borderRightWidth)); 
				div[i].firstChild.style.height=div[i].style.height; div[i].firstChild.style.width=div[i].style.width; 
				break;
			}
		}
	}
}
function getBusyOverlay(parent,overlay,busy) {
	if(typeof(parent)==='object' && document.getElementsByTagName) {
		function parseWidth(val) {return (isNaN(parseInt(val,10))?0:parseInt(val,10));}
		var isIE,isVL,isCV,isWK,isGE,i,b,o,lt,rt,lb,rb,cz,cs,size,inner,outer,string,canvas,context,ctrl,opacity,color,text,styles,waiting=true;
		if(parent.currentStyle){cs=parent.currentStyle;}else if(document.defaultView&&document.defaultView.getComputedStyle){cs=document.defaultView.getComputedStyle(parent,"");}else{cs=parent.style;}
		while(cs.display.search(/block|inline-block|table|inline-table|list-item/i)<0) {parent=parent.parentNode; if(parent.currentStyle){cs=parent.currentStyle;}else if(document.defaultView&&document.defaultView.getComputedStyle){cs=document.defaultView.getComputedStyle(parent,"");}else{cs=parent.style;} if(parent.tagName.toUpperCase()==='BODY') {parent="";}}
		if(typeof(parent)==='object') {
			if(!overlay) {overlay=new Object(); overlay['opacity']=0;} if(!busy) {busy=new Object(); busy['size']=32;}
			opacity=Math.max(0.0,Math.min(1.0,(typeof overlay['opacity']==='number'?overlay['opacity']:0)||0)); color=(typeof overlay['color']==='string'?overlay['color']:'white');
			text=(typeof overlay['text']==='string'?overlay['text']:''); styles=(typeof overlay['style']==='string'?overlay['style']:'');
			canvas=document.createElement("canvas"); isCV=canvas.getContext?1:0; 
			isWK=navigator.userAgent.indexOf('WebKit')>-1?1:0; isGE=navigator.userAgent.indexOf('Gecko')>-1&&window.updateCommands?1:0;
			isIE=navigator.appName=='Microsoft Internet Explorer'&&window.navigator.systemLanguage&&!window.opera?1:0;
			isVL=document.all&&document.namespaces?1:0; outer=document.createElement('div'); 
			parent.style.position=(cs.position=='static'?'relative':cs.position);
			cz=parent.style.zIndex>=0?(parent.style.zIndex-0+2):2;
			if(isIE && !cs.hasLayout) {parent.style.zoom=1;}
			outer.style.position='absolute'; outer.style.overflow='hidden';
			outer.style.display='block'; outer.style.zIndex=cz; 
			outer.style.left=0+'px'; outer.style.top=0+'px';
			outer.style.width='100%'; outer.style.height='100%';
			if(isIE) {outer.className='buzy_ele'; outer.style.zoom=1; outer.style.margin='0px'; outer.style.padding='0px'; outer.style.height=(parent.offsetHeight-parseWidth(cs.borderBottomWidth)-parseWidth(cs.borderTopWidth)); outer.style.width=(parent.offsetWidth-parseWidth(cs.borderLeftWidth)-parseWidth(cs.borderRightWidth));}
			if(typeof(cs.borderRadius)=="undefined"){
				if(typeof(cs.MozBorderRadius)!="undefined"){
					lt=parseFloat(cs.MozBorderRadiusTopleft)-Math.min(parseFloat(cs.borderLeftWidth),parseFloat(cs.borderTopWidth));
					rt=parseFloat(cs.MozBorderRadiusTopright)-Math.min(parseFloat(cs.borderRightWidth),parseFloat(cs.borderTopWidth));
					lb=parseFloat(cs.MozBorderRadiusBottomleft)-Math.min(parseFloat(cs.borderLeftWidth),parseFloat(cs.borderBottomWidth));
					rb=parseFloat(cs.MozBorderRadiusBottomright)-Math.min(parseFloat(cs.borderRightWidth),parseFloat(cs.borderBottomWidth));
					outer.style.MozBorderRadiusTopleft=lt+"px"; outer.style.MozBorderRadiusTopright=rt+"px"; outer.style.MozBorderRadiusBottomleft=lb+"px"; outer.style.MozBorderRadiusBottomright=rb+"px";
				}else if(typeof(cs.WebkitBorderRadius)!="undefined"){
					lt=parseFloat(cs.WebkitBorderTopLeftRadius)-Math.min(parseFloat(cs.borderLeftWidth),parseFloat(cs.borderTopWidth));
					rt=parseFloat(cs.WebkitBorderTopRightRadius)-Math.min(parseFloat(cs.borderRightWidth),parseFloat(cs.borderTopWidth));
					lb=parseFloat(cs.WebkitBorderBottomLeftRadius)-Math.min(parseFloat(cs.borderLeftWidth),parseFloat(cs.borderBottomWidth));
					rb=parseFloat(cs.WebkitBorderBottomRightRadius)-Math.min(parseFloat(cs.borderRightWidth),parseFloat(cs.borderBottomWidth));
					outer.style.WebkitBorderTopLeftRadius=lt+"px"; outer.style.WebkitBorderTopRightRadius=rt+"px"; outer.style.WebkitBorderBottomLeftRadius=lb+"px"; outer.style.WebkitBorderBottomRightRadius=rb+"px";
				}
			}else {
				lt=parseFloat(cs.borderTopLeftRadius)-Math.min(parseFloat(cs.borderLeftWidth),parseFloat(cs.borderTopWidth));
				rt=parseFloat(cs.borderTopRightRadius)-Math.min(parseFloat(cs.borderRightWidth),parseFloat(cs.borderTopWidth));
				lb=parseFloat(cs.borderBottomLeftRadius)-Math.min(parseFloat(cs.borderLeftWidth),parseFloat(cs.borderBottomWidth));
				rb=parseFloat(cs.borderBottomRightRadius)-Math.min(parseFloat(cs.borderRightWidth),parseFloat(cs.borderBottomWidth));
				outer.style.borderTopLeftRadius=lt+"px"; outer.style.borderTopRightRadius=rt+"px"; outer.style.borderBottomLeftRadius=lb+"px"; outer.style.borderBottomRightRadius=rb+"px";
			}
			parent.appendChild(outer);
			inner=document.createElement('div');
			inner.style.position='absolute'; inner.style.cursor='progress';
			inner.style.display='block'; inner.style.zIndex=(cz-1);
			inner.style.left=0+'px'; inner.style.top=0+'px';
			inner.style.width=100+'%'; inner.style.height=100+'%';
			inner.style.backgroundColor=color;
			if(isIE) {inner.style.zoom=1; inner.style.margin='0px'; inner.style.padding='0px'; inner.style.height=outer.style.height; inner.style.width=outer.style.width; }
			if(typeof(cs.borderRadius)=="undefined"){
				if(typeof(cs.MozBorderRadius)!="undefined"){
					inner.style.MozBorderRadiusTopleft=lt+"px"; inner.style.MozBorderRadiusTopright=rt+"px"; inner.style.MozBorderRadiusBottomleft=lb+"px"; inner.style.MozBorderRadiusBottomright=rb+"px";
				}else if(typeof(cs.WebkitBorderRadius)!="undefined"){
					inner.style.WebkitBorderTopLeftRadius=lt+"px"; inner.style.WebkitBorderTopRightRadius=rt+"px"; inner.style.WebkitBorderBottomLeftRadius=lb+"px"; inner.style.WebkitBorderBottomRightRadius=rb+"px";
				}
			}else {
				inner.style.borderTopLeftRadius=lt+"px"; inner.style.borderTopRightRadius=rt+"px"; inner.style.borderBottomLeftRadius=lb+"px"; inner.style.borderBottomRightRadius=rb+"px";
			}
			if(isIE) {inner.style.filter="alpha(opacity="+parseInt(opacity*100)+")";}else {inner.style.opacity=opacity;}
			outer.appendChild(inner); 
			size=Math.max(16,Math.min(512,(typeof busy['size']==='number'?(busy['size']==0?32:busy['size']):32)));
			if(isVL){if(document.namespaces['v']==null) {var stl = document.createStyleSheet(); stl.addRule("v\\:*", "behavior: url(#default#VML);"); document.namespaces.add("v", "urn:schemas-microsoft-com:vml");}}
			if(!isCV){canvas=document.createElement("div");}
			canvas.style.position='absolute'; 
			canvas.style.cursor='progress'; canvas.style.zIndex=(cz-0+1);
			canvas.style.top='50%'; canvas.style.left='50%';
			canvas.style.marginTop='-'+(size/2)+'px';
			canvas.style.marginLeft='-'+(size/2)+'px';
			canvas.width=size; canvas.height=size;
			canvas.style.width=size+"px"; canvas.style.height=size+"px";
			outer.appendChild(canvas);
			if(typeof(text)!=""){
				string=document.createElement('div');
				string.style.position='absolute'; string.style.overflow='hidden'; 
				string.style.cursor='progress'; string.style.zIndex=(cz-0+1);
				string.style.top='50%'; string.style.left='0px';
				string.style.marginTop=2+(size/2)+'px'; string.style.textAlign='center'; 
				string.style.width=100+'%'; string.style.height='auto';
				if(typeof(styles)!=""){
					string.innerHTML='<span '+(styles.match(/:/i)?'style':'class')+'="'+styles+'">'+text+'</span>';
				}else {
					string.innerHTML='<span>'+text+'</span>';
				} outer.appendChild(string);
			}
			if(isGE){
				outer.style.MozUserSelect="none"; inner.style.MozUserSelect="none"; canvas.style.MozUserSelect="none";
			}else if(isWK){
				outer.style.KhtmlUserSelect="none"; inner.style.KhtmlUserSelect="none"; canvas.style.KhtmlUserSelect="none";
			}else if(isIE){
				outer.style.unselectable="on"; inner.style.unselectable="on"; canvas.style.unselectable="on";
			}
			if(isVL){
				ctrl=getBusyVL(canvas,busy['color'],busy['size'],busy['type'],busy['iradius'],busy['weight'],busy['count'],busy['speed'],busy['minopac']); ctrl.start();
			}else if(isCV){
				ctrl=getBusyCV(canvas.getContext("2d"),busy['color'],busy['size'],busy['type'],busy['iradius'],busy['weight'],busy['count'],busy['speed'],busy['minopac']); ctrl.start();
			}else {
				ctrl=getBusy(canvas,busy['color'],busy['size'],busy['type'],busy['iradius'],busy['weight'],busy['count'],busy['speed'],busy['minopac']); ctrl.start();
			}
			if(isIE) { parent.onresize = onIEWinResize; }
			return {remove: function (){if(waiting){waiting=false; ctrl.stop(); delete ctrl; parent.removeChild(outer);} } };
		}
	}
}
function getBusy(obj,cl,sz,tp,ir,w,ct,sp,mo) {
	function getHEX(v){
		var col=v||'#000000';
		if(!col.match(/^#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i)) {
			if(v.match(/^#[0-9a-f][0-9a-f][0-9a-f]$/i)) {col='#'+v.substr(1,1)+v.substr(1,1)+v.substr(2,1)+v.substr(2,1)+v.substr(3,1)+v.substr(3,1);}
		}return col;
	}
	var running=false,i=0,os=0,al=0,f=100,c,h,p,t,x,y,v,hp,ph,sh,ele=new Array();;
	c=getHEX(cl); tp=tp||"t"; t=(tp.match(/^[coprt]/i)?tp.substr(0,1).toLowerCase():'t');
	ct=Math.max(5,Math.min(36,ct||12)); sp=Math.max(30,Math.min(1000,sp||96));
	sz=Math.max(16,Math.min(512,sz||32)); ir=Math.max(1,Math.min((sz/2)-2,ir||sz/4));
	w=Math.max(1,Math.min((sz/2)-ir,w||sz/10)); mo=Math.max(0,Math.min(0.5,mo||0.25));
	al=360/ct; hp=(Math.PI/2)*-1; ph=Math.PI/180; w=(t!='c'?parseInt((w/2)*3):w); v=parseInt((sz/2)-(w/2));		
	for(i=0;i<ct;i++) {
		sh=document.createElement('div');
		x=Math.round(v+v*Math.cos(hp+(i+1)*al*ph)); 
		y=Math.round(v+v*Math.sin(hp+(i+1)*al*ph));
		sh.style.position='absolute'; sh.style.margin='0px';
		sh.style.width=w+'px'; sh.style.height=w+'px';
		sh.style.lineHeight='1px'; sh.style.fontSize='0px';
		sh.style.top=y+'px'; sh.style.left=x+'px'; sh.style.backgroundColor=c;
		if(document.all&&!window.opera) {sh.style.filter="alpha(opacity="+parseInt(Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1))))*100)+")";
		}else {sh.style.opacity=Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1)))); }
		obj.appendChild(sh); ele[i]=sh;
	}
	function nextLoop(){
		if(!running) {return;} os=(os+1)%ct; 
		if(document.all&&!window.opera) {
			for(i=0;i<ct;i++){al=((os+i)%ct); ele[al].style.filter="alpha(opacity="+parseInt(Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1))))*100)+")";}
		}else {
			for(i=0;i<ct;i++){al=((os+i)%ct); ele[al].style.opacity=Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1))));}
		} setTimeout(nextLoop,sp);
	}
	nextLoop(0);
	return {
		start: function (){if(!running){running=true; nextLoop(0);}},
		stop: function (){running=false; for(i=0;i<ct;i++) {if(document.all&&!window.opera) {ele[i].style.filter="alpha(opacity=0)";}else {ele[i].setAttribute('opacity',0);}}},
		pause: function (){running=false; }
	};
}
function getBusyVL(obj,cl,sz,tp,ir,w,ct,sp,mo) {
	function getHEX(v){
		var col=v||'#000000';
		if(!col.match(/^#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i)) {
			if(v.match(/^#[0-9a-f][0-9a-f][0-9a-f]$/i)) {col='#'+v.substr(1,1)+v.substr(1,1)+v.substr(2,1)+v.substr(2,1)+v.substr(3,1)+v.substr(3,1);}
		}return col;
	}
	var running=false,os=0,al=0,f=100,c,i,h,p,t,x,y,hs,qs,hw,qw,rp,sh,fl,ele=new Array();;
	c=getHEX(cl); tp=tp||"t"; t=(tp.match(/^[coprt]/i)?tp.substr(0,1).toLowerCase():'t');
	ct=Math.max(5,Math.min(36,ct||12)); sp=Math.max(30,Math.min(1000,sp||96));
	sz=Math.max(16,Math.min(512,sz||32)); ir=Math.max(1,Math.min((sz/2)-2,ir||sz/4));
	w=Math.max(1,Math.min((sz/2)-ir,w||sz/10)); mo=Math.max(0,Math.min(0.5,mo||0.25));
	h=(sz/2)-ir; x=sz/2; y=x; al=360/ct; hs=parseInt((sz/2)*f); qs=parseInt(hs/2); 
	hw=parseInt((w/2)*f); qw=parseInt(hw/2); rp=hs-parseInt(ir*f); 
	switch(t) {
		case "c": p='m '+hs+','+(rp-hw)+' ar '+(hs-hw)+','+(rp-hw-hw)+','+(hs+hw)+','+rp+','+(hs-hw)+','+(rp-hw-hw)+','+(hs-hw)+','+(rp-hw-hw)+' e'; break;
		case "p": p='m '+(hs-qw)+',0 l '+(hs-hw)+','+rp+','+(hs+hw)+','+rp+','+(hs+qw)+',0 x e'; break;
		case "o": p='m '+hs+','+(rp-qs)+' ar '+(hs-hw)+',0,'+(hs+hw)+','+rp+','+(hs-hw)+',0,'+(hs-hw)+',0 e'; break;
		case "t": p='m '+(hs-hw)+','+rp+' l '+(hs-hw)+','+hw+' qy '+hs+',0 qx '+(hs+hw)+','+hw+' l '+(hs+hw)+','+rp+' x e'; break;
		default: p='m '+(hs-hw)+',0 l '+(hs-hw)+','+rp+','+(hs+hw)+','+rp+','+(hs+hw)+',0 x e'; break;
	} 
	for(i=0;i<ct;i++) {
		sh=document.createElement('v:shape');
		sh.setAttribute('filled','t');
		sh.setAttribute('stroked','f');
		sh.setAttribute('coordorigin','0,0');
		sh.setAttribute('coordsize',(sz*f)+','+(sz*f));
		sh.setAttribute('path',p);
		sh.style.rotation=(i*al);
		sh.style.position='absolute'; sh.style.margin='0px';
		sh.style.width=sz+'px'; sh.style.height=sz+'px';
		sh.style.top='-1px'; sh.style.left='-1px';
		obj.appendChild(sh);
		fl=document.createElement('v:fill');
		fl.setAttribute('color',c);
		fl.setAttribute('opacity',Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1)))));
		sh.appendChild(fl);
		ele[i]=fl;
	}
	function nextLoop(){
		if(!running) {return;}
		os=(os+1)%ct; 
		for(i=0;i<ct;i++){ al=((os+i)%ct);
			ele[al].setAttribute('opacity',Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1)))));
		}setTimeout(nextLoop,sp);
	}
	nextLoop(0);
	return {
		start: function (){if(!running){running=true; nextLoop(0);}},
		stop: function (){running=false; for(i=0;i<ct;i++) {ele[i].setAttribute('opacity',0);}},
		pause: function (){running=false; }
	};
}
function getBusyCV(ctx,cl,sz,tp,ir,w,ct,sp,mo) {
	function getRGB(v){
		function hex2dec(h){return(Math.max(0,Math.min(parseInt(h,16),255)));}
		var r=0,g=0,b=0; v = v||'#000'; if(v.match(/^#[0-9a-f][0-9a-f][0-9a-f]$/i)) {
			r=hex2dec(v.substr(1,1)+v.substr(1,1)),g=hex2dec(v.substr(2,1)+v.substr(2,1)),b=hex2dec(v.substr(3,1)+v.substr(3,1));
		}else if(v.match(/^#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i)) {
			r=hex2dec(v.substr(1,2)),g=hex2dec(v.substr(3,2)),b=hex2dec(v.substr(5,2));
		} return r+','+g+','+b;
	}
	function drawOval(ctx,w,h){ctx.beginPath(); ctx.moveTo(-w/2,h/2); ctx.quadraticCurveTo(-w/2,0,0,0); ctx.quadraticCurveTo(w/2,0,w/2,h/2); ctx.quadraticCurveTo(w/2,h,0,h); ctx.quadraticCurveTo(-w/2,h,-w/2,h/2); ctx.fill();}
	
	function drawTube(ctx,w,h){ctx.beginPath(); ctx.moveTo(w/2,0); ctx.lineTo(-w/2,0); ctx.lineTo(-w/2,h-(w/2)); ctx.quadraticCurveTo(-w/2,h,0,h); ctx.quadraticCurveTo(w/2,h,w/2,h-(w/2)); ctx.fill();}
	
	function drawPoly(ctx,w,h){ctx.beginPath(); ctx.moveTo(w/2,0); ctx.lineTo(-w/2,0); ctx.lineTo(-w/4,h); ctx.lineTo(w/4,h); ctx.fill();}

	function drawCirc(ctx,r,z){ctx.beginPath(); ctx.arc(r,r,r,0,Math.PI*2,false); ctx.fill();}  
	var running=false,os=0,al=0,c,i,h,t,x,y;
	c=getRGB(cl); tp=tp||"t"; t=(tp.match(/^[coprt]/i)?tp.substr(0,1).toLowerCase():'t');
	ct=Math.max(5,Math.min(36,ct||12)); sp=Math.max(30,Math.min(1000,sp||96));
	sz=Math.max(16,Math.min(512,sz||32)); ir=Math.max(1,Math.min((sz/2)-2,ir||sz/4));
	w=Math.max(1,Math.min((sz/2)-ir,w||sz/10)); mo=Math.max(0,Math.min(0.5,mo||0.25));
	h=(sz/2)-ir; x=sz/2; y=x;
	
	function nextLoop(){
		if(!running) {return;}
		os=(os+1)%ct; ctx.clearRect(0,0,sz,sz); ctx.save(); ctx.translate(x,y);
		for(i=0;i<ct;i++){ al=2*((os+i)%ct)*Math.PI/ct; 
			ctx.save(); ctx.translate(ir*Math.sin(-al),ir*Math.cos(-al)); ctx.rotate(al);
			ctx.fillStyle='rgba('+c+','+Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1))))+')';
			switch(t) {case "c": drawCirc(ctx,w/2,h); break; case "o": drawOval(ctx,w,h); break; case "p": drawPoly(ctx,w,h); break; case "t": drawTube(ctx,w,h); break; default: ctx.fillRect(-w/2,0,w,h); break;} ctx.restore();
		} ctx.restore();
		setTimeout(nextLoop,sp);
	}
	nextLoop(0);
	return {
		start: function (){if(!running){running=true; nextLoop(0);}},
		stop: function (){running=false; ctx.clearRect(0,0,sz,sz); },
		pause: function (){running=false; }
	};
}

opal.widget.AjaxHelper = new Class({
    url: '/oval/Dispatcher?',
    pageName: '',
    actionName: '',
    staticParams: '',
    dynamicParams: '',
    lockedCtrl: null,
    afterGetData: null,
    getParams: function(){
  		var lresult = '';		
        if (!(this.staticParams == '')) 
            lresult = '&' + this.staticParams;
        if (!(this.dynamicParams == '')) 
            lresult = '&' + this.dynamicParams;					 
        return lresult;    
    },
    getFullUrl: function(){
        return this.url + 'pagename=' + this.pageName + '&actionname=' + this.actionName + this.getParams()
    },
    
    requestData: function(aHelper, owner, sender){
        if (aHelper.lockedCtrl) {
            var ctrl = getBusyOverlay(aHelper.lockedCtrl, {
                color: '#A8A8A3',
                opacity: 0.5
            });
        };
        
        new Request({
            url: this.getFullUrl(), 
            onComplete: function(responseText, responseXML){
                var data = JSON.decode(responseText);
                if (data === "Session Time Out!") {
                    location.href = "/oval/SessionError.jsp?ajaxErr=Y&threadxcnt=0.2530613679102215";
                    return;
                };

                if (this.afterGetData) 
                    this.afterGetData.call(owner, data, sender);
                
            }
.bind(this)
        }).send(this.getParams);
        destroyLoading(ctrl);
    }
});

var TreeViewFuncs = new Object();
opal.widget.TreeView = function(options){
	options.id = $pick(options.id, 111111);
    var treeView = Element('div', {
		id: options.id,
        'class': 'tree'		
	});
	treeView.idName = '';
	treeView.textName = '';
	treeView.isBottomName = '';
	treeView.DataPackName = '';	
		
	opal.widget.Initialize.call(treeView, options);
	
    treeView.setOptions({
        enableFilter: true,
        onValueLoadComplete: $empty,
        onValueChanged: $empty
    });
	
    $extend(treeView, TreeViewFuncs);
    
    treeView.addEvent('onRequestData', treeView.doRequestData);
    treeView.addEvent('onSelect', treeView.doSelect);
	
	treeView.ajaxHelper = new opal.widget.AjaxHelper();	
	treeView.ajaxHelper.lockedCtrl = treeView;
	treeView.ajaxHelper.afterGetData = treeView.doGetData;


    opal.widget.Render.call(treeView, treeView);
    
    return treeView;
};


opal.widget.TreeView.image = new Object();
opal.widget.TreeView.image.path = '';
opal.widget.TreeView.image.expand = 'treenode_expand.gif';
opal.widget.TreeView.image.collapse = 'treenode_collapse.gif';
opal.widget.TreeView.image.leaf = 'treenode_bottom.gif';

TreeViewFuncs.iniRoot = function (aText) {
	if (!this.root){		
	    this.root = new opal.widget.treeNode({text: aText});
		this.root.treeView = this;
		this.root.inject(this);			  	
	}
	else
		this.root.setText(aText);				
}

TreeViewFuncs.getNodeByKey = function () {
	return null;
}

TreeViewFuncs.getNodesByText = function () {
	return null;	
}

TreeViewFuncs.doRequestData = function (senderNode) {
	if (this.onRequestData){
		this.onRequestData(senderNode, this.ajaxHelper);
	}
    this.ajaxHelper.requestData(this.ajaxHelper, this, senderNode);
}

TreeViewFuncs.doGetData = function (data, senderNode) {  
	if (!senderNode) return;
	if (this.idName == '') return;
	if (this.textName == '') return;	
	if (this.isBottomName == '') return;
	if (this.DataPackName == '') return;	

    var lArray = data[this.DataPackName];
    if (lArray) {
		if (this.onGetData)
		    this.onGetData(lArray);
			
		senderNode.addChildren(lArray);
	};	  		
}

TreeViewFuncs.doSelect = function (senderNode) {
	if (this.onSelect){
		this.onSelect(senderNode);
	}	
}

var TreeNodeFuncs = new Object();
opal.widget.treeNode = function(options){
	options = $pick(options, {});
	options.id = $pick(options.id, opal.generateId());
	options.text = $pick(options.text, '');
		
    var treeNode = Element('div', {
		id: options.id
	});
	
    treeNode.treeView = null;
	treeNode.expended = false;
	treeNode.innerIsBottom = $pick(options.isBottom, false);
	treeNode.loaded = false;

    if (treeNode.innerIsBottom)
    	treeNode.image = new Element('img', {src: opal.widget.TreeView.image.path + opal.widget.TreeView.image.leaf});		
    else
    	treeNode.image = new Element('img', {src: opal.widget.TreeView.image.path + opal.widget.TreeView.image.collapse});		
	
	treeNode.text = new Element('span', {text: options.text});	
	treeNode.text.addClass('treeNode');
    treeNode.container = new Element('div',{
		'id': opal.generateId(),
		'class': 'hide-display'
	});
	treeNode.image.inject(treeNode);
	treeNode.text.inject(treeNode);
	treeNode.container.inject(treeNode);	
	
    opal.widget.Initialize.call(treeNode, options);
	
	treeNode.mother = $pick(options.mother, null);
	if (treeNode.mother){
        treeNode.treeView = treeNode.mother.treeView;
		treeNode.inject(treeNode.mother.container);
		treeNode.innerLavel = treeNode.mother.innerLavel + 1;		 
	}else{
		treeNode.innerLavel = 0;			
	}
	
	treeNode.set('style', 'text-indent: ' + treeNode.innerLavel * 13 + 'pt');
	
    treeNode.setOptions({
        enableFilter: true,
        onValueLoadComplete: $empty,
        onValueChanged: $empty
    });
    $extend(treeNode, TreeNodeFuncs);
    
    treeNode.image.addEvent('click', treeNode.doImgClick.bind(treeNode));	
    treeNode.text.addEvent('click', treeNode.doTextClick.bind(treeNode));
	    
    return treeNode;
};

TreeNodeFuncs.setText = function (aText) {
	this.text.set('text', aText);
}

TreeNodeFuncs.addChildren = function(childNodes){
    if ($type(childNodes) === 'array' && childNodes.length > 0) {
        childNodes.each(function(item, index){
			var lisbottom = item[this.treeView.isBottomName]== 'Y' || item[this.treeView.isBottomName]== 'y';
            var lnode = new opal.widget.treeNode({mother: this, id: item[this.treeView.idName], 
			                                      text: item[this.treeView.textName], isBottom: lisbottom});			
			
			if (this.treeView.onNodeGetData){
				this.treeView.onNodeGetData(lnode, item)
			}			
        }.bind(this)); 
		this.loaded = true;
    };
}

TreeNodeFuncs.expend = function () {
	if (!this.loaded){
		this.treeView.fireEvent('onRequestData', this);
	};

    this.image.src = opal.widget.TreeView.image.path + opal.widget.TreeView.image.expand;	
	this.container.removeClass('hide-display');	
	this.expended = true;
};

TreeNodeFuncs.collapse = function () {
    this.image.src = opal.widget.TreeView.image.path + opal.widget.TreeView.image.collapse;

	this.container.addClass('hide-display');	
	this.expended = false;	
};

TreeNodeFuncs.doImgClick = function () {
	if (!this.innerIsBottom){
		if (this.expended) {
			this.collapse()
		} else {
			this.expend();
		};			
	};
};

TreeNodeFuncs.doUnSelect = function () {
	this.text.removeClass('treeNodeSelected');
	this.text.addClass('treeNode');	
}

TreeNodeFuncs.doSelect = function () {	
	this.text.removeClass('treeNode');
	this.text.addClass('treeNodeSelected');	
}

TreeNodeFuncs.doTextClick = function () {
	if (this.treeView.selectedNode){
    	this.treeView.selectedNode.doUnSelect();		
	}
	
	this.treeView.selectedNode = this;
	this.doSelect();
	this.treeView.fireEvent('onSelect', this);
}

TreeNodeFuncs.clearChildren = function () {
	this.collapse();	
	this.container.empty();
	this.loaded = false;
}